package cn.uniqueinfo.dp.util;

import cn.uniqueinfo.dp.exception.UQException;

/**
 * @author:朱元勋
 * @mail:395910063@qq.com
 * @date:2019-01-14 09:24:30
 * @description: 查询sql 元数据
 */
public class MetaDataUtil {

    /** 校验sql */
    public static boolean check(String sql) {
        if (sql == null || sql.trim().length() == 0) {
            throw new UQException(0, "CheckSql", "sql为空");
        }
        return true;
    }

    /** 获取数据表字段类型对应的java类型简写 */
    public static String getJavaType(String columnType) {

        if (columnType == null || columnType.trim().length() == 0) {
            return "";
        }

        columnType = columnType.toLowerCase().replace("unsigned", "").trim();
        // todo 数据表字段与java类型还没有匹配完善
        if ("char".equals(columnType)
                || "varchar".equals(columnType)
                || "text".equals(columnType)) {
            return "String";
        } else if ("bigint".equals(columnType)) {
            return "Long";
        } else if ("date".equals(columnType)
                || "timestamp".equals(columnType)
                || "datetime".equals(columnType)) {
            return "Date";
        } else if ("smallint".equals(columnType)
                || "int".equals(columnType)
                || "bit".equals(columnType)
                || "tinyint".equals(columnType)) {
            return "Integer";
        } else {
            return "";
        }
    }

    public static void main(String args[]) {
        System.out.println("--------start-----------");
        System.out.println(getJavaType("BIGINT   UNSIGNED"));
        System.out.println("---------end------------");
    }
}
